package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.HomePageAlgo;
import com.cray.intelligenthydrologyback.client.domain.entity.HomePageDeviceCount;
import com.cray.intelligenthydrologyback.client.domain.entity.HomePageSensor;
import com.cray.intelligenthydrologyback.client.domain.entity.SysAlgorithm;
import com.cray.intelligenthydrologyback.client.model.AbstractResponseResult;
import com.cray.intelligenthydrologyback.client.model.ResponseListResult;
import com.cray.intelligenthydrologyback.client.model.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/system/home")
@Api(value = "System Home Page Controller", tags = "系统首页")
public class SystemHomeController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/algo/list")
    @ApiOperation(httpMethod = "POST", value = "获取分组算法列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<HomePageAlgo> getAlgoList(
            @ApiParam(value = "homePageAlgo", required = true)
            @RequestBody HomePageAlgo homePageAlgo) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/home/algo/list")
                        .build(homePageAlgo)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @PostMapping("/device/count")
    @ApiOperation(httpMethod = "POST", value = "获取设备个数统计")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<HomePageDeviceCount> getDeviceCount(
            @ApiParam(value = "homePageDeviceCount", required = true)
            @RequestBody HomePageDeviceCount homePageDeviceCount) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/home/device/count")
                        .build(homePageDeviceCount)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @PostMapping("/sensor/list")
    @ApiOperation(httpMethod = "POST", value = "获取传感器列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<HomePageSensor> getSensorList(
            @ApiParam(value = "homePageSensor", required = true)
            @RequestBody HomePageSensor homePageSensor) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/home/sensor/list")
                        .build(homePageSensor)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }
}
